#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
--------------------------------------
"""
__author__ = 'JiChao_Song'
__data__ = '2020/6/5 下午10:53'

from datetime import datetime

from django.http import JsonResponse


class JsonMessage:
    def __init__(self, code=20000, message='请求成功', data=None, errors=None):
        self.code = code
        self.message = message
        self.data = [] if data is None else data
        self.errors = [] if errors is None else errors
        self.systemTime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    def jsonExit(self):

        data = {
            'code': self.code,
            'message': self.message,
            'data': self.data,
            'errors': self.errors,
            'systemTime': self.systemTime
        }

        if self.errors is None or len(self.errors) == 0:
            del data['errors']
            return JsonResponse(data)
        else:
            data['message'] = '系统异常'
            data['code'] = 50000
            return JsonResponse(data)


class PageResponse(JsonMessage):

    def __init__(self, total=None, page=None, size=None, data= None):
        JsonMessage.__init__(self)
        self.total = total
        self.page = page
        self.size = size
        self.data = data

    def jsonExit(self):

        data = {
            'code': self.code,
            'message': self.message,
            'total': self.total,
            'page': self.page,
            'size': self.size,
            'data': self.data,
            'errors': self.errors,
            'systemTime': self.systemTime
        }

        if self.errors is None or len(self.errors) == 0:
            del data['errors']
            return JsonResponse(data)
        else:
            data['message'] = '系统异常'
            data['code'] = 50000
            return JsonResponse(data)
